using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace Tools.DB
{
    public class clstools : clsDefault
    {
        public static DbParameter CreateParameter(ref Database db, string ParameterName, DbType Tipo, object Value)
        {
            System.Data.Common.DbParameter Par = db.DbProviderFactory.CreateParameter();
            Par.ParameterName = ParameterName;
            Par.DbType = Tipo;
            Par.Value = Value;
            return Par;
        }
        public static string ConvertDataTableToXML(DataTable dtData, string TagName)
        {
            DataSet dsData = new DataSet("ROOT");
            StringBuilder sbSQL;
            StringWriter swSQL;
            string XMLformat;

            sbSQL = new StringBuilder();
            swSQL = new StringWriter(sbSQL);
            dsData.Merge(dtData, true, MissingSchemaAction.AddWithKey);
            dsData.Tables[0].TableName = TagName.ToUpper();
            foreach (DataColumn col in dsData.Tables[0].Columns)
            {
                col.ColumnMapping = MappingType.Attribute;
                col.ColumnName = col.ColumnName.ToUpper();
                col.Caption = col.Caption.ToUpper();
            }
            dsData.WriteXml(swSQL, XmlWriteMode.WriteSchema);
            XMLformat = sbSQL.ToString();
            return XMLformat;

        }
        public static void set_SourceColumn_NameParam(ref Database db, ref DbDataAdapter adp, string proc)
        {
            DbCommand DbC = db.GetStoredProcCommand(proc);
            DbC.Connection = db.CreateConnection();
            adp.UpdateCommand = DbC;
            adp.InsertCommand = DbC;
            adp.DeleteCommand = DbC;
            DbC.Parameters.Clear();
            db.DiscoverParameters(DbC);
            DbParameterCollection dblist = DbC.Parameters;

            foreach (DbParameter dp in dblist)
            {
                dp.SourceColumn = dp.ParameterName.Replace("@", "");
            }
        }
        public static void set_SourceColumn_NameParam(ref Database db
            , ref DbCommand DbC
            , ref DataTable dt)

        {
            
            //DbC.Connection = db.CreateConnection();
            //DbC.Parameters.Clear();
            db.DiscoverParameters(DbC);
            
            DbParameterCollection dblist = DbC.Parameters;
            //db.AddInParameter(DbC, "@USUARIO", DbType.String, apelido);
            if (dt.Rows.Count == 1)
            {
                foreach (DbParameter dp in dblist)
                {
                    if (dp.ParameterName.ToUpper() != "@RETURN_VALUE")
                    {
                        //db.AddInParameter(DbC, dp.ParameterName, dp.DbType, dt.Rows[0][dp.ParameterName.Replace("@", "")].ToString());
                        dp.Value = dt.Rows[0][dp.ParameterName.Replace("@", "")];
                    }
                    //dp.SourceColumn = dp.ParameterName.Replace("@", "");
                }
            }
        }
    }


}
